package com.drug.wms.service.impl;

import com.alibaba.fastjson.JSON;
import com.drug.common.utils.DateUtils;
import com.drug.wms.domain.BusOverflowOrder;
import com.drug.wms.domain.BusOverflowOrderDetails;
import com.drug.wms.domain.BusOverflowOrderVO;
import com.drug.wms.domain.BusSkuStock;
import com.drug.wms.mapper.BusOverflowOrderDetailsMapper;
import com.drug.wms.mapper.BusOverflowOrderMapper;
import com.drug.wms.mapper.BusSkuStockMapper;
import com.drug.wms.service.IBusOverflowOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;


/**
 * 报损报溢总单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-05
 */
@Service
@Slf4j
public class BusOverflowOrderServiceImpl implements IBusOverflowOrderService {
    @Resource
    private BusOverflowOrderMapper busOverflowOrderMapper;

    @Resource
    private BusSkuStockMapper busSkuStockMapper;

    @Resource
    private BusOverflowOrderDetailsMapper busOverflowOrderDetailsMapper;

    /**
     * 查询报损报溢总单
     *
     * @param id 报损报溢总单主键
     * @return 报损报溢总单
     */
    @Override
    public BusOverflowOrder selectBusOverflowOrderById(String id) {
        return busOverflowOrderMapper.selectBusOverflowOrderById(id);
    }

    /**
     * 查询报损报溢总单列表
     *
     * @param busOverflowOrder 报损报溢总单
     * @return 报损报溢总单
     */
    @Override
    public List<BusOverflowOrder> selectBusOverflowOrderList(BusOverflowOrderVO busOverflowOrder) {
        return busOverflowOrderMapper.selectBusOverflowOrderList(busOverflowOrder);
    }

    /**
     * 新增报损报溢总单
     *
     * @param busOverflowOrder 报损报溢总单
     * @return 结果
     */
    @Override
    public int insertBusOverflowOrder(BusOverflowOrder busOverflowOrder) {
        busOverflowOrder.setCreateTime(DateUtils.getNowDate());
        return busOverflowOrderMapper.insertBusOverflowOrder(busOverflowOrder);
    }

    /**
     * 修改报损报溢总单
     *
     * @param orderId 总单id
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBusOverflowOrder(String orderId) {
        List<BusOverflowOrderDetails> detailsList = busOverflowOrderDetailsMapper.selectBusOverflowOrderDetailsById(orderId);
        //提交状态需更新库存
        updateSkuStock(detailsList);
        BusOverflowOrder busOverflowOrder = new BusOverflowOrder();
        busOverflowOrder.setId(orderId);
        busOverflowOrder.setInputStatus(1);
        return busOverflowOrderMapper.updateBusOverflowOrder(busOverflowOrder);
    }



    private void updateSkuStock(List<BusOverflowOrderDetails> detailsList) {
        detailsList.forEach(bean ->
        {
            BusSkuStock busSkuStock = busSkuStockMapper.selectBusSkuStockById(bean.getSkuStockId());
            if(busSkuStock == null ){
                log.info("查询库存失败:{}", JSON.toJSONString(bean));
                return;
            }
            if (bean.getDataType() == 1) {
                busSkuStock.setAmount(busSkuStock.getAmount().add(bean.getQuantity()));
                busSkuStock.setCanSaleAmount(busSkuStock.getCanSaleAmount().add(bean.getQuantity()));
            } else {
                busSkuStock.setAmount(busSkuStock.getAmount().add(bean.getQuantity().multiply(new BigDecimal("-1"))));
                busSkuStock.setCanSaleAmount(busSkuStock.getCanSaleAmount().add(bean.getQuantity().multiply(new BigDecimal("-1"))));
            }
            busSkuStockMapper.updateBusSkuStock(busSkuStock);
        });


    }

    /**
     * 批量删除报损报溢总单
     *
     * @param id 需要删除的报损报溢总单主键
     * @return 结果
     */
    @Override
    public int deleteBusOverflowOrderByIds(String id) {
        BusOverflowOrder busOverflowOrder = new BusOverflowOrder();
        busOverflowOrder.setId(id);
        busOverflowOrder.setValidityCode(1);

        return busOverflowOrderMapper.updateBusOverflowOrder(busOverflowOrder);
    }



}
